//package com.allwees.bs.c.common.auth.config;
//
//import com.allwees.core.common.util.encrypt.MD5Util;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.security.oauth2.common.util.OAuth2Utils;
//import org.springframework.security.oauth2.provider.OAuth2Authentication;
//import org.springframework.security.oauth2.provider.OAuth2Request;
//import org.springframework.security.oauth2.provider.token.AuthenticationKeyGenerator;
//
//import java.math.BigInteger;
//import java.util.LinkedHashMap;
//import java.util.Map;
//
///**
// * <ol>用于控制单点登录:
// * 		<li>手机app 和web 版可以同时登录</li>
// * 		<li>手机app 重新获取token 会删除所有老的token 让其他client失效</li>
// * 		<li>web 版登录不会影响其他client 的登录状态</li>
// * 		<li>web 版 和app token 不一样，所以退出登录不影响其他client</li>
// * </ol>
// */
//@Slf4j
//@Configuration
//public class CustomAuthenticationKeyGenerator implements AuthenticationKeyGenerator {
//	private static final String CLIENT_ID = "client_id";
//
//	private static final String SCOPE = "scope";
//
//	private static final String USERNAME = "username";
//
//	private static final String SOURCE_TYPE = "sourceType";
//
//	@Override
//	public String extractKey(OAuth2Authentication authentication) {
//		Map<String, String> values = new LinkedHashMap<>();
//		OAuth2Request authorizationRequest = authentication.getOAuth2Request();
//		if(!authentication.isClientOnly()) {
//			String username = authentication.getName();
//			values.put(USERNAME, username);
//		}
//		values.put(CLIENT_ID, authorizationRequest.getClientId());
//		if(authorizationRequest.getScope() != null) {
//			values.put(SCOPE, OAuth2Utils.formatParameterList(authorizationRequest.getScope()));
//		}
//
//		// 获取来源地
//		String sourceType = authorizationRequest.getRequestParameters().get(SOURCE_TYPE);
//		if (StringUtils.isNotBlank(sourceType)) {
//			values.put(SOURCE_TYPE, sourceType);
//		}
//
//		String bytes = MD5Util.digest(values.toString());
//		return String.format("%032x", new BigInteger(1, bytes.getBytes()));
//	}
//}
